//
//  GXKMultipleViewController.m
//  GXKPhotos
//
//  Created by 乐善街 on 16/6/17.
//  Copyright © 2016年 tianchuanshangwu. All rights reserved.
//

#import "GXKMultipleViewController.h"
#import "GXKPhotoDatas.h"
#import "GXKListModel.h"
#import "GXKPhotoController.h"
#import "PicCollectionViewCell.h"
#import "GXKPhotoListViewController.h"
@interface GXKMultipleViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>{

    NSInteger photoNum;//第几个相册
}
//从图库获取所有的相册集
@property(nonatomic ,strong)NSMutableArray *photoDatasArray;
//获取每个相册中所有的图片
@property(nonatomic,strong)NSMutableArray *photoPicsArray;

@property(nonatomic,strong)NSMutableArray *selectArray;

@property(nonatomic, strong)UIButton *TitleButton;

@property (strong, nonatomic) UICollectionView *picsCollection;

@end

@implementation GXKMultipleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _TitleButton=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    
    [_TitleButton addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.titleView=_TitleButton;
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"下一步" style:UIBarButtonItemStylePlain target:self action:@selector(nextStep)];
    
     self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    
    
    GXKPhotoController *photoController = [[GXKPhotoController alloc]init];
    photoController.selectPhotoOfMax = 8;
    //设置相册中完成按钮旁边小圆点颜色。
    photoController.roundColor = [UIColor greenColor];
    
    [photoController showIn:self];
    
    photoController.result=^(BOOL isAuth){
        if (isAuth) {
            GXKPhotoDatas *photoDatas=[[GXKPhotoDatas alloc]init];
            _photoDatasArray=[photoDatas GetPhotoListDatas];
            [self requarePhotos:0];
        }
        
    };


    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    CGFloat photoSize = ([UIScreen mainScreen].bounds.size.width - 3*16) / 4;
    flowLayout.minimumInteritemSpacing = 1.0;//item 之间的行的距离
    flowLayout.minimumLineSpacing = 1.0;//item 之间竖的距离
    flowLayout.itemSize = (CGSize){photoSize,photoSize};

    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    _picsCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height-64) collectionViewLayout:flowLayout];
    
    UINib *cellNib = [UINib nibWithNibName:@"PicCollectionViewCell" bundle:nil];
    [_picsCollection registerNib:cellNib forCellWithReuseIdentifier:@"simpleCell"];


    _picsCollection.delegate = self;
    _picsCollection.dataSource = self;
    _picsCollection.backgroundColor = [UIColor whiteColor];
    [_picsCollection setUserInteractionEnabled:YES];
    _picsCollection.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:_picsCollection];
 
}

-(void)back{

     [self dismissViewControllerAnimated:YES completion:nil];
    
}

-(void)nextStep{

  

}





-(void)requarePhotos:(NSInteger)num{
    photoNum=num;
    GXKListModel *listModel=_photoDatasArray[num];
//    self.title=listModel.title;
    
    [_TitleButton setTitle:listModel.title forState:UIControlStateNormal];
    [_TitleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    GXKPhotoDatas *photoDatas=[[GXKPhotoDatas alloc]init];
    _photoPicsArray=[photoDatas GetPhotoAssets:[photoDatas GetFetchResult:listModel.assetCollection]];
   [_picsCollection reloadData];

}

-(NSMutableArray *)selectArray
{
    if (!_selectArray) {
        _selectArray = [NSMutableArray array];
    }
    return _selectArray;
}

-(void)test{
    GXKPhotoListViewController *listViewController=[[GXKPhotoListViewController alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [listViewController show:_photoDatasArray];
    __weak typeof(self)weakSelf=self;
    listViewController.block=^(NSInteger num){
        [weakSelf requarePhotos:num];
    };
}


#pragma UICollectionView --- Datasource

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
  
    return _photoPicsArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *cellIdentifier = @"simpleCell";
    PicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.isSelectedButton.tag = indexPath.row;
    [cell.isSelectedButton addTarget:self action:@selector(selectPicBtn:) forControlEvents:UIControlEventTouchUpInside];
        [cell loadPhotoData:[_photoPicsArray objectAtIndex:indexPath.row]];
    [cell selectBtnStage:self.selectArray existence:[_photoPicsArray objectAtIndex:indexPath.row]];
    
    return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"123");

}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"Deselect");

}


#pragma mark 关键位置，选中的在数组中添加，取消的从数组中减少
-(void)selectPicBtn:(UIButton *)button
{
    NSInteger index = button.tag;
    if (button.selected == NO) {
    
        if (self.selectArray.count + 1 > _maxMultiple) {
            [self showSelectPhotoAlertView:_maxMultiple];
        }else{
            [self.selectArray addObject:[_photoPicsArray objectAtIndex:index]];
          
            [button setImage:[UIImage imageNamed:@"select_yes.png"] forState:UIControlStateNormal];
            button.selected = YES;
        }
    }else{

        [self.selectArray removeObject:[_photoPicsArray objectAtIndex:index]];
        NSLog(@"selectArray %lu",(unsigned long)self.selectArray.count);
        [button setImage:[UIImage imageNamed:@"select_no.png"] forState:UIControlStateNormal];
        button.selected = NO;
    }
}


-(void)showSelectPhotoAlertView:(NSInteger)photoNumOfMax
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提醒" message:[NSString stringWithFormat:@"最多只能选择%ld张图片",(long)photoNumOfMax]preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
        
    }];
    
    [alert addAction:action1];
    [self presentViewController:alert animated:YES completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
